package com.source.lightfowl.basedata.controller;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.BarcodeMachineDownloadDTO;
import com.source.lightfowl.DTO.BarcodeMachineUploadDTO;
import com.source.lightfowl.DTO.PoultryCategoryDownloadDTO;
import com.source.lightfowl.DTO.PoultryCategoryUploadDTO;
import com.source.lightfowl.VO.BarcodeMachineVO;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.Barcode_Machine_Status;
import com.source.lightfowl.basedata.service.BarcodeMachineService;
import com.source.lightfowl.basedata.service.BarcodeMachineStatusService;
import com.source.lightfowl.handler.MyException;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.person.entity.Person;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 12:16
 */
@Api(value = "脚标机管理",tags = "脚标机管理")
@RestController
@CrossOrigin
@RequestMapping
public class  BarcodeMachineController {
    @Autowired
    private BarcodeMachineService barcodeMachineService;
    @Autowired
    private BarcodeMachineStatusService barcodeMachineStatusService;


    //TO DO 2. 卖出角脚标机数量  已测试通过
    @ApiOperation(value = "查询卖出的角标机数量")
    //查找全部
    @GetMapping("/getBarcodeMachineCount")
    public MyResult getCount(){

        Long all = barcodeMachineService.getCount();
        if(all != null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }



    @ApiOperation(value = "查询全部数据")
    //查找全部
    @GetMapping("/barcodeMachine")
    public MyResult findAll(){

        List<BarcodeMachineVO> all = barcodeMachineService.findAll();
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    /*
        疑问：如果参数的页面数超过数据库的数据  要怎么显示？
     */
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
    @GetMapping("/barcodeMachineFindAllPage")
    public MyResult findAllPage(int page,int pageNum){

        Page<BarcodeMachine> all = barcodeMachineService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    /*
        疑问：如果查找的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/barcodeMachine/id")
    public MyResult findById(String id){
        BarcodeMachineVO house = barcodeMachineService.findById(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    //增加
    @ApiOperation(value = "增加/修改 数据 ")
    @PostMapping("/barcodeMachine")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(BarcodeMachine barcodeMachine, HttpServletRequest request){
        try {
            barcodeMachineService.save(barcodeMachine);
            barcodeMachineStatusService.editBarcodeMachineStatus(
                    new Barcode_Machine_Status(barcodeMachine.getId(),"0", barcodeMachine.getUserId())
            );
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(0, "操作失败");
        }

        return MyResult.build(1,"操作成功");

    }


    //修改
    @ApiOperation(value = "增修改 数据 ")
    @PostMapping("/updateBarcodeMachine")
    @LogAnnotation(operEvent = "操作类型修改",operType = 2)
    public MyResult update(BarcodeMachine barcodeMachine, HttpServletRequest request){
        barcodeMachineService.save(barcodeMachine);
        return MyResult.build(1,"操作成功");

    }




    //删除
    /*
        疑问：如果删除的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation("根据id删除数据")
    @DeleteMapping("/barcodeMachine/id")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(String id){
        try{
            barcodeMachineService.deleteById(id);
            return MyResult.build(0,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.build(1,"删除操作出现异常");
        }
    }



    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/barcodeMachineDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的

        try {
            barcodeMachineService.deleteBatch(ids);
            return MyResult.build(0,"批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "批量删除失败");
        }

    }

    //上传
    @ApiOperation(value = "脚标机数据上传")
    @PostMapping("/barcodeMachineUpload")
    public MyResult upload(MultipartFile file,HttpServletRequest request){
        String fileName = file.getOriginalFilename(); //文件名
        String fileSuffix = fileName.substring(fileName.length()-5); //.xlsx
        String fileSuffix1 = fileName.substring(fileName.length()-4); //.xls
        if (!fileSuffix.equals(".xlsx")&&!fileSuffix1.equals(".xls")){
            return MyResult.build(500,"文件格式错误");
        }
        try {
            EasyExcel.read(file.getInputStream(), BarcodeMachineUploadDTO.class,new UploadExcelListener(barcodeMachineService,request)).sheet().doRead();
        }catch (IOException e){
            e.printStackTrace();
            throw new MyException(500,"文件错误");
        }
        return MyResult.build(200,"上传成功");
    }

    //下载
    @ApiOperation(value = "脚标机数据下载")
    @GetMapping("/barcodeMachineDownload")
    public MyResult download(HttpServletResponse response){
        try {
            DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
            downloadExcelHandler.download(response,barcodeMachineService, BarcodeMachineDownloadDTO.class);
        }catch (Exception e){
            return MyResult.no();
        }
        return MyResult.ok();
    }
}
